package client;

import clientGui.*;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Arrays;

import javax.swing.JOptionPane;

public class Client
{
	private static MainWindow mainWindow;
	private static ClientNetService netSrvc;
	
	private static void listen()
	{
		new Thread(
			new Runnable()
			{
				@Override
				public void run()
				{
					while(true)
					{
						try
						{
							handleInput(netSrvc.readLine());
							
						} catch (Exception e)
						{
							showDialog("Utraco połączenie z serwerem");
							mainWindow.getChatPanel().disableInput();
							return;
						}
					}
				}
			}
		).start();
	}
	
	private Client() {}
	
	public static void newIncomingMesssage(String author, String msg)
	{
		mainWindow.getChatPanel().printMessage(author, msg);
	}
	
	public static void connect(String login, String hostname, int port)
	{
		try
		{
			System.out.println("Connecting...");
			netSrvc = new ClientNetService(login, hostname, port);
			System.out.println("Connected to host.");
		} catch (UnknownHostException e)
		{
			showDialog("Nie udało się odnaleźć serwera.");
			return;
		} catch (IOException e)
		{
			showDialog("Nie udało się połączyć z serwerem.");
			return;
		} catch (UsernameTakenException e)
		{
			showDialog("Login '" + login + "' jest zajęty.");
			return;
		}
		mainWindow.showChat();
		listen();
	}
	
	private static void handleInput(String text)
	{
		System.out.println("Got: " + text);
		String [] splitText = text.split(" ", 2);
		System.out.println("Split to: " + Arrays.toString(splitText));
		if(splitText[0].equals("UL")) updateUsersList(splitText[1].split(" "));
		else if(splitText.length == 2)
		{
			boolean priv = splitText[0].equals("MP");
			splitText = splitText[1].split(" ", 2);
			System.out.println("Split to: " + Arrays.toString(splitText));
			if(splitText.length == 2)
			{
				if(priv) splitText[0] = ">> " + splitText[0] + " >>";
				newIncomingMesssage(splitText[0], splitText[1]);
			}
		}
	}
	
	public static void updateUsersList(String [] usersList)
	{
		mainWindow.getUsersPanel().setUsers(usersList);
	}
	
	private static void sendPrivateMsg(String recipient, String msg)
	{
		netSrvc.printLine("MP " + recipient + " " + msg);
	}
	
	private static void sendPublicMsg(String msg)
	{
		netSrvc.printLine("MA " + msg);
	}

	public static void handleUserInput(String text)
	{
		if(!text.startsWith("@@")) sendPublicMsg(text);
		else if(text.indexOf(' ') != -1)
		{
			text = text.substring(2);
			String [] splitText = text.split(" ", 2);
			if(splitText.length == 2) sendPrivateMsg(splitText[0], splitText[1]);
			newIncomingMesssage("<< " + splitText[0] + " <<", splitText[1]);
		}
	}
	
	public static void showDialog(String msg)
	{
		JOptionPane.showMessageDialog(mainWindow, msg);
	}
	
	public static void stop()
	{
		System.out.println("Client preparing to be closed...");
		new Thread(
			new Runnable()
			{
				@Override
				public void run()
				{
					netSrvc.close();
				}
			}
		);
	}
	
	public static void main(String [] args)
	{
		mainWindow = new MainWindow();
	}
}
